<?php

/**
 * This is the model class for table "block".
 *
 * The followings are the available columns in table 'block':
 * @property integer $id
 * @property string $name
 * @property string $code
 * @property integer $user_id
 * @property integer $address_id
 *
 * The followings are the available model relations:
 * @property Address $address
 * @property User $user
 */
class Block extends ActiveRecord
{

    public $addressSearch;
    public $userSearch;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Block the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'block';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array_merge(parent::rules(), array(
                array('name, code, user_id, address_id', 'required', 'message'=>'Kötelező kitölteni'),
                array('code', 'unique', 'allowEmpty'=>false, 'message'=>'Már létezik ilyen háztömb kód.'),
                array('user_id, address_id', 'numerical', 'integerOnly'=>true),
                array('name, code', 'length', 'max'=>255),
                array('code', 'match', 'pattern'=> '/^([A-Z0-9_]*)$/', 'allowEmpty'=>false, 'message'=>'Csak nagybetű, szám, és _ (aláhúzás) lehet.'),
                 array('code', 'unique', 'criteria'=>array(
	                'condition'=>'user_id = :userId',
	                'params'=>array(
	                        ':userId'=>$this->user_id
	                )
	        ), 'message'=>'A kód egyedi kell legyen.'),
                array('name, code, user_id, address_id', 'safe', 'on'=>'search'),
        ));
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
                'address' => array(self::BELONGS_TO, 'Address', 'address_id'),
                'user' => array(self::BELONGS_TO, 'User', 'user_id'),
                'flats'=>array(self::HAS_MANY, 'Flat', 'block_id',
                        'order'=>'flats.number ASC'),
                'flatCount'=>array(self::STAT, 'Flat', 'block_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
                'id' => 'ID',
                'name' => 'Név',
                'code' => 'Kód',
                'user_id' => 'Felhasználó',
                'address_id' => 'Cím',
                'flatCount' => 'Lakások'
        );
    }

    public function owned($user)
    {
        $this->getDbCriteria()->mergeWith(array(
                'condition' => 'user_id= :userId',
                'params' => array(':userId'=>  is_numeric($user) ? $user : $user->id)
        ));
        return $this;
    }

    protected function searchFields(){
        return array('name', 'code');
    }
}